<template>
	<view class="home">
		<u-sticky offset-top="0">
			<view class="tophead">
				<uni-icons type="left" color="#666" size="26" @click="retFn"></uni-icons>
				<view class="">
					轨道式测流车
				</view>
				<view class="" style="width: 200rpx;">
					<u-button type="error" shape="circle" size="mini" text="点击连接设备" @click="tobelFn" color="#f90057"
						v-if="lanname == ''"></u-button>
					<u-button type="error" shape="circle" size="mini" text="设备已连接" @click="tobelFn" color="#00c55c"
						v-if="lanname != ''"></u-button>
				</view>

				<view class="" style="width: 100rpx;display: flex;justify-content: flex-end;align-items: center;">
					<view class="" style="margin-right: 20rpx;">
						<uni-icons type="gear-filled" color="#666" size="26" @click="allocationFn"></uni-icons>
					</view>
				</view>
			</view>
		</u-sticky>
		<!-- 内容 -->
		<view class="concent_box">
			<view class="inibox">
				
				<view class="inibox_but" hover-class="inibox_acv" @click="resettingFn">
					复位
				</view>
				<view class="inibox_but" hover-class="inibox_acv" @click="testFn">
					设备初始化
				</view>
			</view>
			<!-- 故障灯 -->
			<view class="lamp" v-if="lanname != ''">
				<view class="lamp_l">

				</view>
				<view class="lamp_r" @click="lampRFn">

				</view>
			</view>
			<view class="lamp_a" v-if="lanname == ''">
				<view class="lamp_l_a">

				</view>
				<view class="lamp_r_a">

				</view>
			</view>
			<!-- 遥控器 -->
			<view class="remoteControlUnit">
				<view class="con_box">
					<view class="top_b" hover-class="acv_b" @touchstart="riseDevFn" @touchend="riseDevFnTHyy">
						<u-icon name="arrow-up-fill" :color="icColor" size="50"></u-icon>
					</view>
					<view class="com_b">
						<view class="coe_r" hover-class="acv_b" @touchstart="reversalFn" @touchend="riseDevFnTHxx">
							<u-icon name="play-left-fill" :color="icColor" size="50"></u-icon>
						</view>
						<view class="cen_box">
							<view class="flx_c">
								<view class="nub">
									{{nubxx}}
								</view>
							</view>
							<u-line></u-line>
							<view class="flx_c">
								<view class="nub" style="color: #aaff7f;">
									{{nubyy}}
								</view>
							</view>
						</view>
						<view class="coe_r" hover-class="acv_b" @touchstart="forwordFn" @touchend="riseDevFnTHxx">
							<u-icon name="play-right-fill" :color="icColor" size="50"></u-icon>
						</view>
					</view>
					<view class="bom_b" hover-class="acv_b" @touchstart="inwaterFnbm" @touchend="riseDevFnTHyy">
						<u-icon name="arrow-down-fill" :color="icColor" size="50"></u-icon>
					</view>
				</view>
			</view>
			<!-- 摆臂 -->
			<view class="swinging_arm">
				<view class="custom" hover-class="custom_av" @touchstart="hoistUp" @touchend="pauseFn">
					<view class="" style="color: #ffaa00;">
						摆臂升
					</view>
					<u-icon name="arrow-upward" color="#ffaa00" size="30"></u-icon>
				</view>
				<view @click="comeDown" class="custom" hover-class="custom_av" @touchstart="comeDown"
					@touchend="pauseFn">
					<view class="" style="color: #55ff7f;">
						摆臂降
					</view><u-icon name="arrow-downward" color="#55ff7f" size="30"></u-icon>
				</view>
			</view>
			<view class="yuan">
				<view class="yuan_a1" hover-class="yuan_a_av1" @tap="$u.debounce(stopFn, 300)"
					:style="{ pointerEvents: pointerWater}">
					<view class="text">
						紧急
					</view>
					<view class="text">
						停止
					</view>
				</view>
				<view class="yuan_a2" hover-class="yuan_a_av2" @tap="$u.debounce(inquiryFn, 300)"
					:style="{ pointerEvents: pointerMeasure}">
					<view class="text">
						开始
					</view>
					<view class="text">
						测量
					</view>
				</view>
			</view>
			<view class="realtime">
				<view class="i_a" style="color: #dd5e04;">
					当前水深：
				</view>
				<view class="i_b" style="color: #ff6923;">
					{{wD}}
				</view>
			</view>
		</view>
		<!-- 弹出弹出层 -->
		<u-popup :show="showpop" mode="left">
			<view class="classpop">
				<view class="classpop_tit">
					配 置 测 量 参 数
				</view>
				<view class="conpop">
					<view class="popitem">
						<view class="poptext">
							安装高度：
						</view>
						<u--input clearable type="number" placeholder="输入安装高度" border="surround"
							v-model="cfgCeZhan.installH"></u--input>
					</view>
					<view class="popitem">
						<view class="poptext">
							实际水深：
						</view>
						<u--input clearable type="number" placeholder="输入实际水深" border="surround"
							v-model="cfgCeZhan.depthOfWater"></u--input>
					</view>
					<view class="popitem">
						<view class="poptext">
							探头入水深度：
						</view>
						<u--input clearable type="number" placeholder="输入探头入水深度" border="surround"
							v-model="cfgCeZhan.thoroughH"></u--input>
					</view>
					<view class="popitem">
						<view class="poptext">
							距离水面高度：
						</view>
						<u--input clearable type="number" placeholder="输入距离水面高度" border="surround"
							v-model="cfgCeZhan.waterH"></u--input>
					</view>
				</view>
			</view>
			<view class="butpop">
				<view class="" style="padding: 30rpx;">
					<u-button @click="confirmFn" type="success" text="确定"></u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import colorGradient from '../../uni_modules/uview-ui/libs/function/colorGradient'
	import dayjs from '@/common/dayjs.min.js'
	export default {
		data() {
			return {
				icColor: '#232323',
				objCLC: {
					categories: [],
					series: [{
						data: []
					}],
				},
				letviewHistory: {}, //查看历史
				flowCheckpop: false,
				Arrflow: [], //历史记录
				disabledIf: true,
				urlAlphabet: 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict',
				deleteId: null,
				flowHistorypop: false,
				inidev: true,
				liusliangnub: 0, //平均总流量
				timereal: 0, //实时数据
				pointerWater: 'auto',
				pointerMeasure: 'auto',
				showHistoryIF: false,
				titleIF: '是否返回',
				cfgCeZhan: {
					installH: '4.3', //安装高度
					thoroughH: '0.25', //入水深度
					waterH: '0.5', //距离水面高度
					depthOfWater: '2' //实际水深
				},
				valuesel: 0,
				rangesel: [{
						value: 3,
						text: "测线3"
					},
					{
						value: 4,
						text: "测线4"
					},
					{
						value: 5,
						text: "测线5"
					},
					{
						value: 6,
						text: "测线6"
					},
					{
						value: 7,
						text: "测线7"
					},
					{
						value: 8,
						text: "测线8"
					},
					{
						value: 9,
						text: "测线9"
					},
				],
				setUp: {},
				showpop: false,
				deviceId: '', //蓝牙
				serviceId: '', //蓝牙
				characteristicId: '', //蓝牙
				fff: '', //蓝牙
				mfvNumArr: [], //获取流速
				sumArr: [], //求平均流速
				sumIfArr: true, //判断平均和数组
				wD: 0, //水深
				count: 0, //计数
				depthWater: [], //水深
				cezhanObj: {},
				lanname: '',
				timerFn: null, //定时器
				nubxx: 0, //横向
				nubyy: 0, //纵向
			}
		},
		onShow() {
			this.lanname = this.vuex_devicename
			this.deviceId = this.vuex_deviceId
			this.getBLEDeviceServices()
			uni.showLoading({
				mask: true,
				title: '获取服务中...'
			});
			setTimeout(() => {
				uni.hideLoading()
			}, 1000)
		},
		onLoad(option) {
			// 测站
			this.cezhanFn()
			// 保持屏幕常亮
			uni.setKeepScreenOn({
				keepScreenOn: true
			});

		},
		methods: {
			// 摆臂升
			hoistUp() {
				let initiate = ['0x05', '0x06', '0x00', '0x02', '0x00', '0xC8', '0x28', '0x18']
				let iniaa = ['0x05', '0x05', '0x00', '0x06', '0xFF', '0x00', '0x6D', '0xBF']
				this.conbuffer(initiate)
				setTimeout(() => {
					this.conbuffer(iniaa)
				}, 300)
			},
			// 摆臂降
			comeDown() {
				let initiate = ['0x05', '0x06', '0x00', '0x02', '0x00', '0x50', '0x29', '0xB2']
				let iniaa = ['0x05', '0x05', '0x00', '0x06', '0xFF', '0x00', '0x6D', '0xBF']
				this.conbuffer(initiate)
				setTimeout(() => {
					this.conbuffer(iniaa)
				}, 300)
			},
			// 暂停摆臂
			pauseFn() {
				let activate = ['0x05', '0x05', '0x00', '0x04', '0xFF', '0x00', '0xCC', '0x7F']
				this.conbuffer(activate)
			},
			// 测站信息
			cezhanFn() {
				// 配置
				this.cfgCeZhan = this.vuex_cfgCeZhan
				this.wD = this.cfgCeZhan.depthOfWater
				// 流速仪
				let liusuyi = this.vuex_liusuyiData
				// 断面参数
				let cezhan = this.vuex_cezhanInfo
				this.cezhanObj = cezhan
			},
			// 确认
			confirmFn() {
				uni.$u.vuex('vuex_cfgCeZhan', this.cfgCeZhan)
				this.wD = this.cfgCeZhan.depthOfWater
				this.showpop = false
			},
			// 紧急暂停
			stopFn() {
				// let xstop = ['0x02', '0x06', '0x60', '0x40', '0x00', '0x07', '0xD7', '0xEF']
				// let xstopb = ['0x01', '0x06', '0x60', '0x40', '0x01', '0x0F', '0xD7', '0x8A']
				let locationRun = ['0x00', '0x06', '0x60', '0x40', '0x01', '0x0F', '0xD6', '0x5B']
				this.conbuffer(locationRun)
				setTimeout(() => {
					this.pauseFn()
				}, 300)

			},
			// 复位
			resettingFn(){
				let activate = ['0x05', '0x05', '0x00', '0x05', '0xFF', '0x00', '0x9D', '0xBF']
				this.conbuffer(activate)
			},
			// 脉冲清零归零模式
			testFn() {
				uni.showToast({
					title: '正在初始化...',
					icon: 'loading',
					mask: true,
					duration: 10000
				});
				// 驱动器上电初始化
				let ini = ['0x01', '0x06', '0x60', '0x40', '0x00', '0x01', '0x57', '0xDE']
				// 驱动器正常运行，但电机未使能，松开刹车
				let brakeDel = ['0x01', '0x06', '0x60', '0x40', '0x00', '0x03', '0xD6', '0x1F']
				// 电机给电，处于可运行状态
				let electric = ['0x01', '0x06', '0x60', '0x40', '0x00', '0x0F', '0xD6', '0x1A']
				// 设置回零模式 01 06 60 60 00 06 17 D6
				let hpos = ['0x01', '0x06', '0x60', '0x60', '0x00', '0x06', '0x17', '0xD6']
				// 设定回零方式 01 10 60 7C 00 08 10 00 00 03 E8 00 01 00 00 00 64 03 E8 00 00 00 64
				let hposTop = ['0x01', '0x10', '0x60', '0x7C', '0x00', '0x08', '0x10', '0x00', '0x00', '0x03', '0xE8',
					'0x00', '0x01', '0x00', '0x00', '0x00', '0x64', '0x03', '0xE8', '0x00', '0x00', '0x00', '0x64',
					'0x0D', '0xBE'
				]
				// 开始归零
				let startToZero = ['0x01', '0x06', '0x60', '0x40', '0x00', '0x1F', '0xD7', '0xD6']
				this.conbuffer(ini)
				setTimeout(() => {
					this.conbuffer(brakeDel)
				}, 300)
				setTimeout(() => {
					this.conbuffer(electric)
				}, 600)
				setTimeout(() => {
					this.conbuffer(hpos)
				}, 900)
				setTimeout(() => {
					this.conbuffer(hposTop)
				}, 1200)
				setTimeout(() => {
					this.conbuffer(startToZero)
					setTimeout(() => {
						this.verticalZeroingYY()
					}, 300)
				}, 1500)
			},
			// 纵向归零
			verticalZeroingYY() {
				// 驱动器上电初始化
				let ini = ['0x02', '0x06', '0x60', '0x40', '0x00', '0x01', '0x57', '0xED']
				// 驱动器正常运行，但电机未使能，松开刹车
				let brakeDel = ['0x02', '0x06', '0x60', '0x40', '0x00', '0x03', '0xD6', '0x2C']
				// 电机给电，处于可运行状态
				let electric = ['0x02', '0x06', '0x60', '0x40', '0x00', '0x0F', '0xD6', '0x29']
				// 设置回零模式 01 06 60 60 00 06 17 D6
				let hpos = ['0x02', '0x06', '0x60', '0x60', '0x00', '0x06', '0x17', '0xE5']
				// 设定回零方式 01 10 60 7C 00 08 10 00 00 03 E8 00 01 00 00 00 64 03 E8 00 00 00 64
				let hposTop = ['0x02', '0x10', '0x60', '0x7C', '0x00', '0x08', '0x10', '0x00', '0x00', '0x03', '0xE8',
					'0x00', '0x01', '0x00', '0x00', '0x00', '0x64', '0x03', '0xE8', '0x00', '0x00', '0x00', '0x64',
					'0x59', '0x5B'
				]
				// 开始归零
				let startToZero = ['0x02', '0x06', '0x60', '0x40', '0x00', '0x1F', '0xD7', '0xE5']
				this.conbuffer(ini)
				setTimeout(() => {
					this.conbuffer(brakeDel)
				}, 300)
				setTimeout(() => {
					this.conbuffer(electric)
				}, 600)
				setTimeout(() => {
					this.conbuffer(hpos)
				}, 900)
				setTimeout(() => {
					this.conbuffer(hposTop)
				}, 1200)
				setTimeout(() => {
					this.conbuffer(startToZero)
					this.xxNubAc = 0
					this.yyNubAc = 0
				}, 1500)
				setTimeout(() => {
					this.landscapeOrientaFn()
				}, 1800)
			},
			// 横向初始化
			landscapeOrientaFn() {
				// uni.showToast({
				// 	title: '正在初始化...',
				// 	icon: 'loading',
				// 	mask: true,
				// 	duration: 5000
				// });
				// 初始化
				let ini = ['0x01', '0x06', '0x60', '0x40', '0x00', '0x01', '0x57', '0xDE']
				// 松开刹车 
				let brakeDel = ['0x01', '0x06', '0x60', '0x40', '0x00', '0x03', '0xD6', '0x1F']
				// 电机给电
				let electric = ['0x01', '0x06', '0x60', '0x40', '0x00', '0x0F', '0xD6', '0x1A']
				// 模式  01 06 60 60 00 03 D7 D5
				let hpos = ['0x01', '0x06', '0x60', '0x60', '0x00', '0x03', '0xD7', '0xD5']
				// 暂停电机运行模式
				let hposTop = ['0x01', '0x06', '0x60', '0x40', '0x01', '0x0F', '0xD7', '0x8A']

				this.conbuffer(ini)
				setTimeout(() => {
					this.conbuffer(brakeDel)
				}, 300)
				setTimeout(() => {
					this.conbuffer(electric)
				}, 600)
				setTimeout(() => {
					this.conbuffer(hpos)
				}, 900)
				setTimeout(() => {
					this.conbuffer(hposTop)
					setTimeout(() => {
						this.portraitFn()
					}, 300)
				}, 1200)
			},
			// 纵向初始化
			portraitFn() {
				// 初始化
				let ini = ['0x02', '0x06', '0x60', '0x40', '0x00', '0x01', '0x57', '0xED']
				// 松开刹车 
				let brakeDel = ['0x02', '0x06', '0x60', '0x40', '0x00', '0x03', '0xD6', '0x2C']
				// 电机给电
				let electric = ['0x02', '0x06', '0x60', '0x40', '0x00', '0x0F', '0xD6', '0x29']
				// 位置模式    02 06 60 60 00 01
				let hpos = ['0x02', '0x06', '0x60', '0x60', '0x00', '0x03', '0xD7', '0xE6']
				// 暂停电机运行模式    01 06 60 40 01 0F D7 8A
				let hposTop = ['0x02', '0x06', '0x60', '0x40', '0x01', '0x0F', '0xD7', '0xB9']
				// 点位参数
				let hposData = ['0x02', '0x10', '0x60', '0x81', '0x00', '0x04', '0x08', '0x00', '0x00', '0x00', '0xC8',
					'0x00', '0x64', '0x00', '0x64', '0x2A', '0x82'
				]

				this.conbuffer(ini)
				setTimeout(() => {
					this.conbuffer(brakeDel)
				}, 300)
				setTimeout(() => {
					this.conbuffer(electric)
				}, 600)
				setTimeout(() => {
					this.conbuffer(hpos)
				}, 900)
				setTimeout(() => {
					this.conbuffer(hposTop)
				}, 1200)
				setTimeout(() => {
					this.conbuffer(hposData)
				}, 1500)
			},
			// 遥控器
			// 横向按钮正转
			forwordFn() {
				console.log('右');
				// 点位参数
				let hposData = ['0x01', '0x10', '0x60', '0x81', '0x00', '0x04', '0x08', '0x00', '0x00', '0x00', '0xC8',
					'0x00', '0x64', '0x00', '0x64', '0x69', '0x83'
				]
				let initiate = ['0x01', '0x06', '0x60', '0x40', '0x00', '0x0F', '0xD6', '0x1A']
				this.conbuffer(hposData)
				setTimeout(() => {
					this.conbuffer(initiate)
				}, 300)
			},
			// 横向按钮反转
			reversalFn() {
				console.log('左');
				let hposData = ['0x01', '0x10', '0x60', '0x81', '0x00', '0x04', '0x08', '0xFF', '0xFF', '0xFF', '0x38',
					'0x00', '0x64', '0x00', '0x64', '0x7D', '0x92'
				]
				let initiate = ['0x01', '0x06', '0x60', '0x40', '0x00', '0x0F', '0xD6', '0x1A']
				this.conbuffer(hposData)
				setTimeout(() => {
					this.conbuffer(initiate)
				}, 100)
			},
			//设备下降
			inwaterFnbm() {
				console.log('下');
				// 点位参数
				let hposData = ['0x02', '0x10', '0x60', '0x81', '0x00', '0x04', '0x08', '0x00', '0x00', '0x00', '0xC8',
					'0x00', '0x64', '0x00', '0x64', '0x2A', '0x82'
				]
				let initiate = ['0x02', '0x06', '0x60', '0x40', '0x00', '0x0F', '0xD6', '0x29']
				this.conbuffer(hposData)
				setTimeout(() => {
					this.conbuffer(initiate)
				}, 100)
			},
			// 设备上升
			riseDevFn() {
				console.log('上');
				// 点位参数
				let hposData = ['0x02', '0x10', '0x60', '0x81', '0x00', '0x04', '0x08', '0xFF', '0xFF', '0xFF', '0x38',
					'0x00', '0x64', '0x00', '0x64', '0x3E', '0x93'
				]
				let initiate = ['0x02', '0x06', '0x60', '0x40', '0x00', '0x0F', '0xD6', '0x29']
				this.conbuffer(hposData)
				setTimeout(() => {
					this.conbuffer(initiate)
				}, 300)
			},
			// 松开暂停
			riseDevFnTHxx() {
				console.log('松开暂停01');
				let pause = ['0x01', '0x06', '0x60', '0x40', '0x01', '0x0F', '0xD7', '0x8A']
				this.conbuffer(pause)
				setTimeout(()=>{
					this.aLLBrakeFnxx()
				},300)
				// clearInterval(this.timerFn);
			},
			riseDevFnTHyy() {
				console.log('松开暂停02');
				let pause = ['0x02', '0x06', '0x60', '0x40', '0x00', '0x07', '0xD7', '0xEF']
				this.conbuffer(pause)
				setTimeout(()=>{
					this.aLLBrakeFnyy()
				},300)
			},
			// 询问横向地址脉冲圈数
			aLLBrakeFnxx() {
				let brake = ['0x01', '0x03', '0x60', '0x64', '0x00', '0x02', '0x9B', '0xD4']
				this.conbuffer(brake)
			},
			// 询问纵向地址脉冲圈数
			aLLBrakeFnyy() {
				let brake = ['0x02', '0x03', '0x60', '0x64', '0x00', '0x02', '0x9B', '0xE7']
				this.conbuffer(brake)
			},
			//开始测流
			inquiryFn() {
				if (this.cezhanObj.cexianlist.length == 0) {
					uni.showToast({
						title: '未选择测线',
						mask: true,
						duration: 1500
					});
					return
				}
				if (this.sumArr.length >= this.cezhanObj.cexianlist.length && this.cezhanObj.cexianlist.length == 0) {
					uni.showToast({
						title: '已无测线数据',
						mask: true,
						duration: 1500
					});
					return
				}
				if (this.sumArr.length == this.cezhanObj.cexianlist.length && this.cezhanObj.cexianlist.length != 0) {
					uni.showToast({
						title: '测量完成',
						mask: true,
						duration: 1500
					});
					return
				}
				this.mfvNumArr = [] //数组100
				this.sumIfArr = true //数组求和平均判断
			},
			// 获取测流数据
			mfvFn(data) {
				let num = data.toFixed(3) / 1000
				if (this.mfvNumArr.length < 200) {
					this.mfvNumArr.push(num)
					this.timereal = num //实时数据
					// 测试图表
					this.objCLC.categories.push(num)
					this.objCLC.series[0].data.push(num)
					// 测试图表
					return
				}
				if (this.sumIfArr) {
					this.removeSmallestN(this.mfvNumArr, 50)
					this.removeMax(this.mfvNumArr, 50)
					let sumnum = {
						aveTy: null
					}
					sumnum.aveTy = this.averageSum(this.mfvNumArr).toFixed(3) * 1
					this.sumArr.push(sumnum)
					this.sumIfArr = false //执行一次
					this.pointerMeasure = 'auto' //允许点击测量
					// this.pointerWater = 'auto' //允许点击设备下水
					this.riseFn() // 水面提升
					this.calculateFlow(this.sumArr)
				}
			},
			// 设备上升
			riseFn() {
				let domStr = this.cezhanObj.installH * 1 - this.wD - this.cezhanObj.waterH * 1
				this.declineFn(domStr)
			},
			// 计算流量
			calculateFlow(dataArr) {
				if (this.wD <= 0) {
					uni.showToast({
						icon: 'fail',
						title: '未获取水深',
						duration: 1500
					});
					return
				}
				let vls = dataArr
				let vlAll = []
				let vl0 = null //区域流量
				let vl1 = null //区域流量
				let vlz = null //区域流量
				let vln = null //区域流量
				let vln1 = null //区域流量
				let vln2 = null //区域流量

				// 不参与循环
				// cot
				let sin27LL = Math.sin(this.cezhanObj.lSA * Math.PI / 180)
				let cos27LL = Math.cos(this.cezhanObj.lSA * Math.PI / 180)
				let areaLL = cos27LL / sin27LL //三角面积L

				let sin27RR = Math.sin(this.cezhanObj.rSA * Math.PI / 180)
				let cos27RR = Math.cos(this.cezhanObj.rSA * Math.PI / 180)
				let areaRR = cos27RR / sin27RR //三角面积R
				// VL0
				vl0 = 0.5 * this.wD * this.wD * areaLL * (vls[0].aveTy * 1) * (this
					.cezhanObj.vmL * 1) * (this.cezhanObj.kL * 1)
				vlAll.push(vl0.toFixed(6) * 1)

				//VL1
				vl1 = 0.5 * [(vls[0].aveTy * 1) * (this.cezhanObj.vmL * 1) + (vls[0].aveTy * 1) * (this.cezhanObj
					.cexianlist[0].vM * 1)] * (this.wD) * (this.cezhanObj.cexianlist[0].cR * 1)
				vlAll.push(vl1.toFixed(6) * 1)


				// 中间逻辑
				if (vls.length > 1) {
					for (let i = 0; i < vls.length - 1; i++) {
						vlz = 0.5 * [(vls[i].aveTy * 1) * (this.cezhanObj.cexianlist[i].vM * 1) + (vls[i + 1].aveTy * 1) *
							(this.cezhanObj.cexianlist[i + 1].vM * 1)
						] * (this.wD) * [(this.cezhanObj.cexianlist[i + 1].cR * 1) - (this.cezhanObj.cexianlist[i].cR *
							1)]
						vlAll.push(vlz.toFixed(6) * 1)
					}
				}


				//VLN+1
				vln1 = 0.5 * [(vls[vls.length - 1].aveTy * 1) * (this.cezhanObj.vmR) + (vls[vls.length - 1]
					.aveTy * 1) * (this.cezhanObj.cexianlist[vls.length - 1].vM * 1)] * (this.wD) * [(this
					.cezhanObj.qudikuandu * 1) - (this.cezhanObj.cexianlist[vls.length - 1].cR * 1)]
				vlAll.push(vln1.toFixed(6) * 1)


				//VLN+2
				vln2 = 0.5 * this.wD * this.wD * areaRR * (vls[vls.length - 1].aveTy * 1) * (this
					.cezhanObj.vmR * 1) * (this.cezhanObj.kR * 1)
				vlAll.push(vln2.toFixed(6) * 1)
				// 计算总平均流速
				let vlQitem = []
				let vlQArr = []
				let vlQALL = []

				let sum = 0;
				for (let i = 0; i < vlAll.length; i++) {
					sum += vlAll[i];
				}
				this.liusliangnub = sum.toFixed(4) * 1
				if (this.sumArr.length == this.cezhanObj.cexianlist.length && this.cezhanObj.cexianlist.length != 0) {
					uni.showToast({
						title: '测量完成',
						mask: true,
						duration: 1500
					});
					this.disabledIf = false
					return
				}
			},
			// 配置
			allocationFn() {
				this.showpop = true
			},
			// 返回
			retFn() {

			},
			// 输入蓝牙数据
			conbuffer(data) {
				let msg = data
				// 向蓝牙设备发送一个0x00的16进制数据
				const buffer = new ArrayBuffer(msg.length)
				const dataView = new DataView(buffer)
				// dataView.setUint8(0, 0)
				for (var i = 0; i < msg.length; i++) {
					// dataView.setUint8(i, msg.charAt(i).charCodeAt())
					dataView.setUint8(i, msg[i]);
				}
				this.sendMsgToKey(buffer)
			},
			//监听消息变化 ******************************************************************************************
			listenValueChange(data) {
				let resHex = data
				setTimeout(() => {
					uni.onBLECharacteristicValueChange(res => {
						let resHex = this.ab2hex(res.value)
						console.log('监听初始化数据', resHex, resHex.length);
						// 获取脉冲xx
						if (resHex.substr(0, 6) == '010304' && resHex.length == 18) {
							let str = resHex.substr(6, 8)
							this.nubxx = (this.hexToDecimal(str) / 10000 / 50 * 0.305).toFixed(2)
						}
						// 获取脉冲yy
						if (resHex.substr(0, 6) == '020304' && resHex.length == 18) {
							let str = resHex.substr(6, 8)
							this.nubyy = (this.hexToDecimal(str) / 10000 / 50 * 0.21775).toFixed(2)
						}
						// 询问水深
						if (resHex.substr(0, 6) == '030302' && resHex.length == 14) {
							let str = resHex.substr(6, 4)
							let nubm = this.hexToDecimal(str)
							this.depthWater.push(nubm)
							this.wD = nubm / 100
							if (this.depthWater.length == 10) {
								let arr = this.depthWater
								this.removeSmallestN(arr, 5)
								this.removeMax(arr, 4)
								this.treatmentDepth(arr[0])
							}
						}
						// 获取流速
						if (resHex.substr(0, 6) == '040408' && resHex.length == 26) {
							let str = resHex.substr(14, 4)
							this.mfvFn(this.hexToDecimal(str))
						}
					})
				}, 300)
			},
			//获取蓝牙的所有服务
			getBLEDeviceServices() {
				setTimeout(() => {
					uni.getBLEDeviceServices({
						// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
						deviceId: this.vuex_deviceId,
						success: (res) => {
							console.log('获取蓝牙所有服务', res);
							//E95D93AF-251D-470A-A062-FA1922DFA9A8
							//6E400001-B5A3-F393-E0A9-E50E24DCCA9E
							res.services.forEach((item) => {
								if (item.uuid.indexOf("0000FFF0") != -1) {
									this.serviceId = item.uuid;
									//获取特征
									this.getBLEDeviceCharacteristics()
								}
								if (item.uuid.indexOf("0000FFE0") != -1) {
									this.serviceId = item.uuid;
									//获取特征
									this.getBLEDeviceCharacteristics()
								}
							})
						}
					})
				}, 1000)
			},
			//获取蓝牙特征
			getBLEDeviceCharacteristics() {
				console.log('获取蓝牙特征值');
				setTimeout(() => {
					uni.getBLEDeviceCharacteristics({
						// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
						deviceId: this.deviceId,
						// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
						serviceId: this.serviceId,
						success: (res) => {
							this.characteristics = res.characteristics
							res.characteristics.forEach((item) => {

								if (item.uuid.indexOf("FFF2") != -1) {
									this.fff = item.uuid
								}
								if (item.uuid.indexOf("FFE1") != -1) {
									this.fff = item.uuid
								}
								if (item.uuid.indexOf("0000FFE1") != -1) {

									this.characteristicId = item.uuid
									//利用传参的形势传给下面的notify，这里的uuid如果都需要用到，就不用做判断了，建议使用setTimeout进行间隔性的调用此方法
									this.notifyBLECharacteristicValueChange(item.uuid)
								}
								if (item.uuid.indexOf("0000FFF1") != -1) {

									this.characteristicId = item.uuid


									//利用传参的形势传给下面的notify，这里的uuid如果都需要用到，就不用做判断了，建议使用setTimeout进行间隔性的调用此方法
									this.notifyBLECharacteristicValueChange(item.uuid)
								}
							})
						},
						fail: (res) => {
							console.log(res)
						}
					})
				}, 1000)
			},
			// 蓝牙
			tobelFn() {
				//在起始页面跳转到test.vue页面并传递参数
				uni.navigateTo({
					url: '/pages/index/devBEL'
				});

			},
			// 10进制转32位二进制补码
			NumberOf1(num) {
				// 获取负数的绝对值
				const absoluteValue = Math.abs(num);

				// 将绝对值转换为32位二进制字符串
				let binaryString = absoluteValue.toString(2);

				// 确保二进制字符串长度为32位
				while (binaryString.length < 32) {
					binaryString = '0' + binaryString;
				}

				// 对二进制字符串取反
				const invertedBinaryString = binaryString.split('').map(bit => bit === '0' ? '1' : '0').join('');

				// 将取反后的二进制字符串转换为数字并加1
				const twosComplement = parseInt(invertedBinaryString, 2) + 1;

				// 再次将补码转换为32位二进制字符串
				binaryString = twosComplement.toString(2);

				// 确保二进制字符串长度为32位
				while (binaryString.length < 32) {
					binaryString = '0' + binaryString;
				}

				return binaryString;
			},
			// 二进制补码转16进制
			binaryToHex(binaryStr) {
				// 确保二进制字符串是32位
				if (binaryStr.length !== 32) {
					throw new Error('Binary string must be 32 bits long');
				}

				// 将二进制字符串转换为数字
				var number = parseInt(binaryStr, 2);

				// 转换为16进制并返回结果
				return number.toString(16);
				// 示例使用
				// var binaryStr = '010101010101010101010101010101010'; // 32位二进制补码
				// var hexStr = binaryToHex(binaryStr);
			},
			//数组求平均
			averageSum(arr) {
				if (arr.length === 0) {
					return 0; // 当数组为空时，平均值为0
				}

				let sum = 0;
				for (let i = 0; i < arr.length; i++) {
					sum += arr[i];
				}

				return sum / arr.length;
			},
			// 长度不够补0
			strRepair(str, nub) {
				let nubm = nub * 1
				let strif = str + ''
				if (strif.length < nubm) {
					for (let i = 0; i < nubm - str.length; i++) {
						strif = '0' + strif
					}
				}
				return strif
			},
			//给每个数组添加0x
			ocstr(data) {
				let arr = []
				for (let i = 0; i < data.length; i++) {
					data[i] = '0x' + data[i]
				}
				return data
			},
			// 每2个字符串为一个元素数组
			chunkStringByTwo(str) {
				const result = [];
				for (let i = 0; i < str.length; i += 2) {
					result.push(str.slice(i, i + 2));
				}
				return result;
			},
			// 处理ASCLL 30 -00
			ascllFn(data) {
				let str = data + ''
				let arr = this.chunkStringByTwo(str)

				for (let i = 0; i < arr.length; i++) {
					if (arr[i] == '00') {
						arr[i] = '30'
					}
				}
				let arrnew = arr

				let strnre = ''
				for (let i = 0; i < arrnew.length; i++) {
					strnre += arrnew[i]
				}
				return strnre
			},
			// CRC16校验
			crc16(dataHexString) {
				const dataBytes = [];
				for (let i = 0; i < dataHexString.length; i += 2) {
					dataBytes.push(parseInt(dataHexString.substr(i, 2), 16));
				}

				let crc = 0xFFFF;
				const polynomial = 0xA001;

				for (const byte of dataBytes) {
					crc ^= byte;
					for (let i = 0; i < 8; i++) {
						if (crc & 0x0001) {
							crc = ((crc >> 1) ^ polynomial) & 0xFFFF;
						} else {
							crc >>= 1;
						}
					}
				}
				let crcstr = crc.toString(16).toUpperCase();
				if (crcstr.length % 2 == 0) {
					let result = crcstr.match(/.{1,2}/g)
					return result;
				} else {
					let str = '0' + crcstr
					let result = str.match(/.{1,2}/g)
					return result;
				}
			},
			// 16进制转10进制
			hexToDecimal(hex) {
				return parseInt(hex, 16);
			},
			// 10进制转16进制
			decimalToHex(data) {
				let decimal = data * 1
				let hex = decimal.toString(16).toUpperCase();
				3
				if (hex.length % 2 !== 0) {
					return '0' + hex
				} else {
					return hex
				}
				// return decimal.toString(16).toUpperCase();
			},
			// 16进制转字符串 ASCII码
			hexToString(hex) {
				let str = '';
				for (let i = 0; i < hex.length; i += 2) {
					let charCode = parseInt(hex.substr(i, 2), 16);
					str += String.fromCharCode(charCode);
				}
				return str;
			},

			//字符串转16进制
			stringToHex(data) {
				let str = data + ''
				return str.split('').map(function(char) {
					return char.charCodeAt(0).toString(16).padStart(2, '0');
				}).join('');
			},
			// 16进制转汉字
			hexToChinese(hex) {
				var str = '';
				for (var i = 0; i < hex.length / 4; i++) {
					var codePoint = parseInt(hex.substr(i * 4, 4), 16); // 获取每四位的十六进制值并转换成十进制
					str += String.fromCharCode(codePoint); // 根据Unicode编码生成对应的字符
				}
				return str;
			},
			// 汉字转16进制
			convertToHex(str) {
				let result = '';
				for (let i = 0; i < str.length; i++) {
					result += str.charCodeAt(i).toString(16);
				}
				return result;
			},
			// 随机数
			getRandomNumber(min, max) {
				return Math.floor(Math.random() * (max - min + 1)) + min;
			},
			// 删除最小的前面几个数字
			removeSmallestN(arr, n) {
				// 对数组进行升序排序
				const sorted = arr.sort((a, b) => a - b);
				// 删除最前面的n个数字
				sorted.splice(0, n);
				return sorted;
			},
			// 删除最大的后面几个数字
			removeMax(arr, n) {
				// 对数组进行升序排序
				const sorted = arr.sort((a, b) => a - b);
				// 删除最后面的n个数字
				sorted.splice(-n);
				return sorted;
			},
			// ArrayBuffer转16进度字符串示例
			ab2hex(buffer) {
				const hexArr = Array.prototype.map.call(
					new Uint8Array(buffer),
					function(bit) {
						return ('00' + bit.toString(16)).slice(-2)
					}
				)
				return hexArr.join('')
			},
			/* 向设备发送消息(分包发送，单包20字节，递归发送)
			 */
			sendMsgToKey(buffer) {
				var that = this //vue中的this
				const packageSize = 20 //分包大小
				if (buffer.byteLength <= 20) { //如果小于20直接发送，不再继续调用
					uni.writeBLECharacteristicValue({
						// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
						deviceId: that.deviceId,
						// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
						serviceId: that.serviceId,
						// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
						characteristicId: that.fff, //第二步写入的特征值
						// 这里的value是ArrayBuffer类型
						value: buffer,
						writeType: 'write',
						success: (res) => {
							//此时设备已接收到你写入的数据
							console.log('写入111');

						},
						fail: (err) => {
							console.log('传输失败', err)
							this.lanname = this.vuex_devicename
							// let text = err.errCode + ''
							// uni.showToast({
							// 	title: text,
							// 	mask: true,
							// 	icon: 'error',
							// 	duration: 1000
							// });
						},
						complete: function() {}
					})
				} else { //如果大于20发送完成后继续递归调用
					var newData = buffer.slice(20)
					var writeBuffer = buffer.slice(0, 20)
					uni.writeBLECharacteristicValue({
						// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
						deviceId: that.deviceId,
						// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
						serviceId: that.serviceId,
						// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
						characteristicId: that.fff, //第二步写入的特征值
						// 这里的value是ArrayBuffer类型
						value: writeBuffer,
						writeType: 'write',
						success: function(res) {
							console.log('写入222');
							//写入后继续递归调用发送剩下的数据
							setTimeout(() => {
								that.sendMsgToKey(newData)
							}, 100)
						},
						fail: (err) => {
							console.log('传输失败', err)
							this.lanname = this.vuex_devicename
							// let text = err.errCode + ''
							// uni.showToast({
							// 	title: text,
							// 	icon: 'exception',
							// 	mask: true,
							// 	duration: 1000
							// });
						},
						complete: function() {}
					})
				}
			},
			// 启用 notify 功能
			notifyBLECharacteristicValueChange(characteristicId) {
				console.log('启用 notify 功能');
				uni.notifyBLECharacteristicValueChange({
					state: true, // 启用 notify 功能
					// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
					deviceId: this.deviceId,

					// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
					serviceId: this.serviceId,
					// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
					characteristicId: this.characteristicId,
					success: (res) => {
						console.log('蓝牙名字', res);
						// vuex
						// uni.$u.vuex('vuex_devicename', this.vuexlist.name)
						uni.hideLoading()
						this.listenValueChange()
						uni.showToast({
							title: '服务已开启',
							mask: true,
							duration: 1000
						});
						console.log('蓝牙服务已开启');
					},
					fail: (res) => {
						uni.hideLoading()
						uni.showToast({
							title: '获取服务失败',
							icon: 'fail',
							mask: true,
							duration: 1000
						});

					}
				})
			},
			//断开连接
			lampRFn() {
				console.log('断开设备', this.vuex_deviceId, this.vuex_devicename);
				uni.closeBLEConnection({
					deviceId: this.vuex_deviceId,
					success: (res) => {
						uni.showToast({
							title: '蓝牙已断开',
							icon: 'success',
							mask: true,
							duration: 1000
						});
						this.lanname = ''
						uni.$u.vuex('vuex_devicename', '')
					}
				})
			},
			// 删除数组
			removeById(arr, id) {
				for (let i = 0; i < arr.length; i++) {
					if (arr[i].id === id) {
						arr.splice(i, 1);
						break;
					}
				}
				return arr;
			},
			// 生Id
			nanoid(size = 21) {
				let id = ''
				let i = size
				while (i--) {
					id += this.urlAlphabet[(Math.random() * 64) | 0]
				}
				return id
			}
		}
	}
</script>
<style scoped lang="scss">
	// 内容
	.concent_box {
		padding: 0 30rpx;

		// 遥控器
		.remoteControlUnit {
			width: 100%;
			height: 710rpx;
			border-radius: 350rpx;
			margin: 20rpx 0;
			background-color: #e4e5e7;
			// background-color: #d0d9e7;
			border: 5rpx solid #fff;
			box-shadow: 0 0 10rpx 10rpx rgba(0, 0, 0, 0.1);

			.con_box {
				height: 100%;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: space-between;



				.top_b {
					width: 200rpx;
					height: 200rpx;
					border-radius: 150rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					background-color: #f1f2f3;

				}

				.acv_b {
					background-color: #c9ffdb;
					box-shadow: 0 0 10rpx 10rpx rgba(76, 255, 0, 0.8);
				}

				.com_b {
					font-family: 'MyFont';
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.coe_r {
						width: 200rpx;
						height: 200rpx;
						border-radius: 100rpx;
						display: flex;
						justify-content: center;
						align-items: center;
						background-color: #f1f2f3;
					}

					.acv_b {
						background-color: #c9ffdb;
						box-shadow: 0 0 10rpx 10rpx rgba(76, 255, 0, 0.8);
					}

					.cen_box {
						width: 250rpx;
						height: 250rpx;
						border-radius: 80rpx;
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						color: #40874a;
						// text-shadow: 0 0 5rpx #72ff8a ;
						font-size: 60rpx;
						background-color: #efeff1;
						border: 5rpx solid #fff;
						box-shadow: 0 0 10rpx 10rpx rgba(0, 0, 0, 0.1);

						.flx_c {
							display: flex;
							justify-content: space-between;
							align-items: center;
							margin: 20rpx 0;

							.nub {
								width: 160rpx;
								margin-left: 10rpx;
								text-align: center;
								background-color: #000000;
								border-radius: 10rpx;
								padding: 0 10rpx;
								color: #aaffff;
							}
						}
					}
				}

				.bom_b {
					width: 200rpx;
					height: 200rpx;
					border-radius: 150rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					background-color: #f1f2f3;
				}

				.acv_b {
					background-color: #c9ffdb;
					box-shadow: 0 0 10rpx 10rpx rgba(76, 255, 0, 0.8);
				}
			}


		}

		// 摆臂
		.swinging_arm {
			display: flex;
			justify-content: space-around;
			align-items: center;
			margin: 30rpx 0 20rpx 0;

			.custom {
				width: 300rpx;
				border-radius: 50rpx;
				padding: 10rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				background-color: #000000;
			}

			.custom_av {
				background-color: #777a6a;
			}
		}

		// 故障灯
		.lamp {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 30rpx;

			.lamp_l {
				width: 50rpx;
				height: 50rpx;
				border-radius: 100rpx;
				background-color: #00ff11;
				// border: 10rpx solid #77ff9e;
				box-shadow: inset 0 0 10rpx rgba(0, 0, 0, 0.3);
			}

			.lamp_r {
				width: 50rpx;
				height: 50rpx;
				border-radius: 100rpx;
				// background-color: #ff0000;
				background-color: #fffcfc;
				box-shadow: inset 0 0 10rpx rgba(0, 0, 0, 0.3);
			}
		}

		.lamp_a {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 30rpx;

			.lamp_l_a {
				width: 50rpx;
				height: 50rpx;
				border-radius: 100rpx;
				background-color: #e6fff4;
				box-shadow: inset 0 0 20rpx rgba(0, 0, 0, 0.3);
			}

			.lamp_r_a {
				width: 50rpx;
				height: 50rpx;
				border-radius: 100rpx;
				background-color: #ff0000;
				box-shadow: inset 0 0 20rpx rgba(0, 0, 0, 0.3);
			}
		}

		.inibox {
			margin-bottom: 20rpx;

			.inibox_but {
				text-align: center;
				color: #515151;
				font-size: 30rpx;
				padding: 10rpx 0;
				border-radius: 200rpx;
				background-color: #f2f7f7;
				border: 10rpx solid #f2f7f7;
				box-shadow: inset 0 0 20rpx rgba(140, 140, 140, 0.4);
			}

			.inibox_acv {
				color: #ffffff;
				background-color: #d0d4d4;
			}
		}

		.butt_box {
			display: flex;
			// align-items: center;
			// justify-content: space-between;
			flex-wrap: wrap;

			.butt_i {
				width: 200rpx;
				margin: 15rpx;
			}
		}

		.yuan {
			display: flex;
			justify-content: space-around;
			align-items: center;

			.yuan_a1 {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				// margin: 10rpx 0;
				width: 220rpx;
				height: 220rpx;
				color: #ffffff;
				font-size: 60rpx;
				border-radius: 200rpx;
				background-color: #ce3141;
				border: 25rpx solid #f1aeb5;
				box-shadow: inset 0 0 30rpx rgba(0, 0, 0, 0.3);

				.text {
					text-align: center;
				}
			}

			.yuan_a_av1 {
				background-color: #ff546e;
				color: #ffc9ca;
			}

			.yuan_a2 {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				// margin: 10rpx 0;
				width: 220rpx;
				height: 220rpx;
				color: #ffffff;
				font-size: 60rpx;
				border-radius: 200rpx;
				background-color: #0590cb;
				border: 25rpx solid #9eeaf9;
				box-shadow: inset 0 0 30rpx rgba(0, 0, 0, 0.3);


				.text {
					text-align: center;
				}
			}

			.yuan_a_av2 {
				background-color: #06b9ff;
				color: #a7e5ff;
			}
		}

		.realtime {
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 40rpx;
			padding: 30rpx;
			margin: 50rpx 0;
			background-color: #fafafa;
			border-radius: 20rpx;
			border: 3rpx solid #fff;
			padding: 10rpx;
			box-shadow: 0 0 15rpx rgba(127, 127, 127, 0.2);

			.i_a {
				color: #304951;
			}

			.i_b {
				font-size: 70rpx;
				text-align: center;
				border-radius: 10rpx;
				width: 320rpx;
				font-family: 'MyFont';
				background-color: #304951;
				color: #33ff9e;
			}

			.i_c {
				margin-left: 20rpx;
				font-size: 40rpx;
			}
		}
	}

	.tophead {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 60rpx 20rpx 10rpx 20rpx;
		color: #000000;
		background-color: #ffffff;
	}

	.classpop {
		width: 600rpx;
		height: 1200rpx;
		overflow: auto;
		padding: 100rpx 50rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.classpop_tit {
			font-size: 50rpx;
			color: #002561;
			background-color: #c8dbe6;
			padding: 20rpx;
			border-radius: 20rpx;
			margin-bottom: 20rpx;
		}

		.conpop {
			.popitem {
				margin-bottom: 10rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.l {
					margin-left: 20rpx;
					width: 200rpx;
				}

				.poptext {
					width: 350rpx;
					text-align: justify;
					text-align-last: justify;
					font-size: 45rpx;
					color: #000000;
				}
			}
		}

		.butpop {
			margin-top: 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-around;
		}
	}
</style>
<style>
	@font-face {
		font-family: 'MyFont';
		src: url('@/static/fonts/LED.otf');
	}

	/* 	page {
		background-color: #ffffff;
	} */
</style>